/*
 * Copyright (c) 2015  3 2
 * ---11 :43:25
 * ---作者:吴培基
 * ---QQ:757671834
 */

package kali.wpj.kali;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.github.johnpersano.supertoasts.SuperToast;
import com.github.johnpersano.supertoasts.util.Style;
import com.marshalchen.common.commonUtils.basicUtils.HandlerUtils;
import com.marshalchen.common.uimodule.ResideMenu.ResideMenu;
import com.marshalchen.common.uimodule.ResideMenu.ResideMenuItem;
import com.marshalchen.common.uimodule.activityanimation.AnimatedRectLayout;
import com.marshalchen.common.uimodule.androidanimations.Techniques;
import com.marshalchen.common.uimodule.androidanimations.YoYo;

import kali.wpj.kali.common.AllRecodeActivity;
import kali.wpj.kali.common.settingActivity;
import kali.wpj.kali.scan.NmapActivity;
import kali.wpj.kali.scan.TchSshDosActivity;
import kali.wpj.kali.scan.VulnerabilityScanningActivity;
import kali.wpj.kali.util.NetWorkUtils;
import kali.wpj.kali.web.WebEvaluationActivity;

import static kali.wpj.kali.MyImageView.OnViewClick;


public class MainActivity extends FragmentActivity implements View.OnClickListener{

    /**
     * Called when the activity is first created.
     */
    private MyImageView joke, nmap, ssl, webEvaluation;
    private boolean isExit;
    private ResideMenu resideMenu;
    private MainActivity mContext;
    private ResideMenuItem itemHome;
    private ResideMenuItem itemProfile;
    private ResideMenuItem itemCalendar;
    private ResideMenuItem itemSettings;
    private TextView txt_mainfenxi,txt_mainfen;
    private YoYo.YoYoString rope,rope1;
    private View mTarget,mTarget2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        joke = (MyImageView) findViewById(R.id.c_ping);
        nmap = (MyImageView) findViewById(R.id.c_nmap);
        ssl = (MyImageView) findViewById(R.id.ssl);
        if(!NetWorkUtils.isConnected(this)){
            SuperToast.create(this,getString(R.string.checknet), SuperToast.Duration.LONG,
                    Style.getStyle(Style.BLUE, SuperToast.Animations.POPUP)).show();


        }else{
            if(!NetWorkUtils.isWifi(this)){
                SuperToast.create(this,getString(R.string.wifiuse), SuperToast.Duration.LONG,
                        Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN)).show();
            }
        }
        webEvaluation = (MyImageView) findViewById(R.id.c_recommend);
        HandlerUtils.sendMessageHandlerDelay(shakeHandler,0,1500);


        txt_mainfenxi=(TextView)findViewById(R.id.txt_mainfenxi);
        txt_mainfen=(TextView)findViewById(R.id.txt_mainfen);
        mTarget=findViewById(R.id.txt_mainfenxi);
        mTarget2=findViewById(R.id.txt_mainfen);
        rope=YoYo.with(Techniques.BounceInRight).duration(3000).playOn(mTarget);
        rope1=YoYo.with(Techniques.BounceInLeft).duration(3000).playOn(mTarget2);
        mContext = this;
        setUpMenu();

        /*****************************************************************************/
        joke.setOnClickIntent(new OnViewClick() {

            @Override
            public void onClick() {
                // TODO Auto-generated method stub
                startActivity(new Intent(MainActivity.this, VulnerabilityScanningActivity.class));
                overridePendingTransition(R.anim.ac_transition_fade_in, R.anim.ac_transition_fade_out);
                finish();

            }
        });
        /*****************************************************************************/
        nmap.setOnClickIntent(new OnViewClick() {
            @Override
            public void onClick() {
                startActivity(new Intent(MainActivity.this, NmapActivity.class));
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
                finish();

            }
        });
        /*******************************************************************************/
        ssl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TchSshDosActivity.class));
                overridePendingTransition(R.anim.ac_transition_fade_in, R.anim.ac_transition_fade_out);
                finish();

            }
        });
        /**************************web evaluation***************************************************/
        webEvaluation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, WebEvaluationActivity.class));
                overridePendingTransition(R.anim.ac_transition_flip_horizontal_in, R.anim.ac_transition_flip_horizontal_out);

            }
        });
    }

    Handler shakeHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            com.marshalchen.common.commonUtils.uiUtils.AnimationUtils.wholeShake(joke).start();
            com.marshalchen.common.commonUtils.uiUtils.AnimationUtils.wholeShake(nmap).start();
            com.marshalchen.common.commonUtils.uiUtils.AnimationUtils.wholeShake(ssl).start();
            com.marshalchen.common.commonUtils.uiUtils.AnimationUtils.wholeShake(webEvaluation).start();

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sql_map, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(MainActivity.this, settingActivity.class));
            overridePendingTransition(R.anim.ac_transition_unzoom_in, R.anim.ac_transition_unzoom_out);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);

        }
    }

    private void exit() {
        if (!(isExit)) {

            isExit = true;
            SuperToast.create(this, getString(R.string.exit), SuperToast.Duration.LONG,
                    Style.getStyle(Style.BLUE, SuperToast.Animations.POPUP)).show();

            handler.sendEmptyMessageDelayed(0, 2000);

        } else {

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };


    @Override
    public void onClick(View view) {
        Intent intent = null;
        if (view == itemHome){
            intent = new Intent(MainActivity.this,HomeFragment.class);
            intent.putExtra("animation_type", AnimatedRectLayout.ANIMATION_RANDOM);
        }else if (view == itemProfile){
            intent = new Intent(MainActivity.this,AllRecodeActivity.class);
            intent.putExtra("animation_type", AnimatedRectLayout.ANIMATION_WAVE_BL);
        }else if (view == itemCalendar){
            intent = new Intent(MainActivity.this,CalendarFragment.class);
            intent.putExtra("animation_type", AnimatedRectLayout.ANIMATION_WAVE_TL);
        }else if (view == itemSettings){
            intent = new Intent(MainActivity.this,SettingsFragment.class);
            intent.putExtra("animation_type", AnimatedRectLayout.ANIMATION_WAVE_BR);
        }
        startActivity(intent);
        overridePendingTransition(0,0);
        resideMenu.closeMenu();
    }
    private void setUpMenu() {

        // attach to current activity;
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.drawable.resize_menu_menu_background);
        resideMenu.attachToActivity(this);
        resideMenu.setMenuListener(menuListener);
        //valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip.
        resideMenu.setScaleValue(0.6f);

        // create menu items;
        itemHome     = new ResideMenuItem(this, R.drawable.resize_menu_icon_home,     getString(R.string.say));
        itemProfile  = new ResideMenuItem(this, R.drawable.resize_menu_icon_calendar,  getString(R.string.r));
        itemCalendar = new ResideMenuItem(this, R.drawable.resize_menu_icon_profile, getString(R.string.about));
        itemSettings = new ResideMenuItem(this, R.drawable.resize_menu_icon_settings, getString(R.string.s));

        itemHome.setOnClickListener(this);
        itemProfile.setOnClickListener(this);
        itemCalendar.setOnClickListener(this);
        itemSettings.setOnClickListener(this);
        resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
        resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);

        // You can disable a direction by setting ->
        // resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

//        findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
//            }
//        });
//        findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
//            }
//        });
    }
    @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }


    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {
        }

        @Override
        public void closeMenu() {
        }
    };

//    private void changeFragment(Fragment targetFragment){
//        resideMenu.clearIgnoredViewList();
//        getSupportFragmentManager()
//                .beginTransaction()
//                .replace(R.id.main_fragment, targetFragment, "fragments")
//                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
//                .commit();
//
//    }

    // What good method is to access resideMenu？
    public ResideMenu getResideMenu(){
        return resideMenu;
    }


}
